package GUI;

import com.mysql.cj.jdbc.exceptions.SQLError;
import com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping;
import 数据库和网络.myDatebase;
import 试题和试卷.question;
import 试题和试卷.selectionQuestion;
import 试题和试卷.selectionQuestionBaseItem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

public class addSelectionWindow extends JFrame {
    JPanel mainPanel;
    JScrollPane qScroller,aScroller,bScroller,cScroller,dScroller;
    JButton addButton,backButton;
    JLabel questionCont;
    JLabel aCont ;
    JLabel bCont ;
    JLabel cCont;
    JLabel dCont ;
    JLabel answerCont;
    JTextArea title,difficulty,score,questionContent,answer,aContent,cContent,dContent,bContent;

    public static void main(String[] args) {
        new addSelectionWindow();
    }
    public addSelectionWindow(){
        mainPanel= new JPanel();
        mainPanel.setLayout(null);

        JLabel questionCont = new JLabel("题干：");
        JLabel aCont = new JLabel("A选项：");
        JLabel bCont = new JLabel("B选项：");
        JLabel cCont = new JLabel("C选项：");
        JLabel dCont = new JLabel("D选项：");
        JLabel answerCont = new JLabel("答案：");
        addButton = new JButton("添加");
        backButton = new JButton("返回");

        JLabel questionTitle = new JLabel("标题");
        title = new JTextArea(1,5);
        JLabel questionDifficulty = new JLabel("难度：");
        difficulty = new JTextArea(1,5);
        JLabel questionScore = new JLabel("满分：");
        score = new JTextArea(1,5);

        addButton.setBackground(Color.lightGray);
        addButton.addActionListener(new addListener());
        backButton.setBackground(Color.lightGray);
        backButton.addActionListener(new backListener());

        answer = new JTextArea(1,5);

        questionContent = new JTextArea(6,20);
        questionContent.setText("qContent");
        questionContent.setLineWrap(true);
        questionContent.setWrapStyleWord(true);
        //questionContent.setFont(bigFont);

        qScroller = new JScrollPane(questionContent);
        qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        aContent = new JTextArea(1,20);
        aContent.setText("aContent");
        aContent.setLineWrap(true);
        aContent.setWrapStyleWord(true);
        //questionContent.setFont(bigFont);

        aScroller = new JScrollPane(aContent);
        aScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        aScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        bContent = new JTextArea(1,20);
        bContent.setText("bContent");
        bContent.setLineWrap(true);
        bContent.setWrapStyleWord(true);
        //questionContent.setFont(bigFont);

        bScroller = new JScrollPane(bContent);
        bScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        bScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        cContent = new JTextArea(1,20);
        cContent.setText("cContent");
        cContent.setLineWrap(true);
        cContent.setWrapStyleWord(true);
        //questionContent.setFont(bigFont);

        cScroller = new JScrollPane(cContent);
        cScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        cScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        dContent = new JTextArea(1,20);
        dContent.setText("dContent");
        dContent.setLineWrap(true);
        dContent.setWrapStyleWord(true);
        //questionContent.setFont(bigFont);

        dScroller = new JScrollPane(dContent);
        dScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        dScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);


        questionTitle.setBounds(30,30,50,20);
        title.setBounds(100,30,300,30);
        questionCont.setBounds(30,70,50,20);
        qScroller.setBounds(100,70,300,100);
        aCont.setBounds(30,180,50,20);
        aScroller.setBounds(100,180,300,30);
        bCont.setBounds(30,220,50,20);
        bScroller.setBounds(100,220,300,30);
        cCont.setBounds(30,260,50,20);
        cScroller.setBounds(100,260,300,30);
        dCont.setBounds(30,300,50,20);
        dScroller.setBounds(100,300,300,30);
        answerCont.setBounds(30,340,50,20);
        answer.setBounds(100,340,300,30);
        questionDifficulty.setBounds(30,380,50,20);
        difficulty.setBounds(100,380,300,30);
        questionScore.setBounds(30,420,50,20);
        score.setBounds(100,420,300,30);
        addButton.setBounds(110,490,70,30);
        backButton.setBounds(280,490,70,30);


        mainPanel.add(questionTitle);
        mainPanel.add(title);
        mainPanel.add(questionCont);
        mainPanel.add(qScroller);
        mainPanel.add(aCont);
        mainPanel.add(aScroller);
        mainPanel.add(bCont);
        mainPanel.add(bScroller);
        mainPanel.add(cCont);
        mainPanel.add(cScroller);
        mainPanel.add(dCont);
        mainPanel.add(dScroller);
        mainPanel.add(answerCont);
        mainPanel.add(answer);
        mainPanel.add(addButton);
        mainPanel.add(questionDifficulty);
        mainPanel.add(difficulty);
        mainPanel.add(questionScore);
        mainPanel.add(score);
        mainPanel.add(backButton);




        this.setTitle("添加选择题");
        this.setContentPane(mainPanel);
        this.setSize(450,610);
        mainPanel.setSize(this.getSize());
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        //this.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    }
    public class backListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            addSelectionWindow.this.dispose();

        }
    }
    public class addListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            myDatebase editor=new myDatebase("questioncollection");
            String sql="insert into questioncollection.selection_question (questiontitle, questiontext, questiondifficulties, fullscore, option_a, option_b,option_c,option_d, answer) " +
                    "values ('"+title.getText()+"','"+questionContent.getText()+"','"+difficulty.getText()+"','"+score.getText()+"','"+aContent.getText()+"','"+bContent.getText()+"','"+cContent.getText()+"','"+dContent.getText()+"','"+answer.getText()+"')";

            try {
                editor.statement.execute(sql);
            } catch (SQLException throwables) {
                new warningForSyntexWindow("a");
                throwables.printStackTrace();
            }
            editor.close();
            addSelectionWindow.this.dispose();
            questionWindow.mainPanel.removeAll();
            questionWindow.mainPanel.updateUI();


            }
        }
}


